package found

import "github.com/slclub/go-tips"

type Item struct {
	ID       uint32
	Text     string
	Document Document
	Score    int
}

type Document struct {
	Prompt     string
	Completion string
}

type Data struct {
	Time      float64
	Total     int
	Page      int
	Limit     int
	Documents []*Item
	Words     []string
}

func (self *Data) ConvFromMap(m map[string]any) {
	if val, ok := m["time"]; ok {
		self.Time, _ = val.(float64)
	}
	if val, ok := m["total"]; ok {
		self.Total = tips.Int(val)
	}
	if val, ok := m["Page"]; ok {
		self.Page = tips.Int(val)
	}
	if val, ok := m["limit"]; ok {
		self.Limit = tips.Int(val)
	}
	if val, ok := m["words"]; ok {
		words_any, _ := val.([]any)
		for _, v := range words_any {
			self.Words = append(self.Words, tips.String(v))
		}
	}

	if self.Documents == nil {
		self.Documents = []*Item{}
	}
	self.Documents = ConvArray2Item(m["documents"])
}

// functions
func ConvArray2Item(v any) []*Item {
	rtn := []*Item{}
	varr, ok := v.([]any)
	if !ok {
		return rtn
	}

	for _, va := range varr {
		vm, ok := va.(map[string]any)
		if !ok {
			continue
		}
		item := &Item{
			ID:    uint32(tips.Any2Int64(vm["id"])),
			Text:  tips.String(vm["text"]),
			Score: tips.Int(vm["score"]),
		}
		dmmm, ok := vm["document"].(map[string]any)
		item.Document.Prompt = tips.String(dmmm["prompt"])
		item.Document.Completion = tips.String(dmmm["completion"])
		rtn = append(rtn, item)
	}
	return rtn
}
